﻿Imports System.Reflection
Imports System.Reflection.Emit
Imports VBParser

Namespace Compiler.Semantic
    ''' <summary>
    ''' Children of a compile unit. A compile unit element could be a namespace, a type or other elements under compiling.
    ''' </summary>
    ''' <remarks></remarks>
    Public MustInherit Class CompileUnitElementDescription
        Inherits NameContainer

        Protected Sub New(ByVal parent As NameContainer)
            MyBase.New(parent)

            Dim parentCompileUnit = TryCast(parent, CompileUnitDescription)
            Dim parentSyntaxNode = TryCast(parent, CompileUnitElementDescription)

            If parentCompileUnit IsNot Nothing Then
                'parent is compile unit
                m_compileUnit = parentCompileUnit
            ElseIf parentSyntaxNode IsNot Nothing Then
                'parent is a type or namespace
                m_compileUnit = parentSyntaxNode.CompileUnit
            Else
                'unknown parent type
                Debug.Assert(False, "The parent node of a CompileUnitElementDescription node is unknown. Must be a bug.")
            End If
        End Sub


        Private m_compileUnit As CompileUnitDescription
        Public ReadOnly Property CompileUnit() As CompileUnitDescription
            Get
                Return m_compileUnit
            End Get
        End Property

        'Semantic analysis
        Public Overridable Function DeclareType(ByVal syntaxNode As Declaration) As TypeDescription
            Debug.Assert(syntaxNode IsNot Nothing)

            Dim newDescription As TypeDescription

            If TypeOf syntaxNode Is ClassDeclaration Then
                newDescription = New ClassDescription(Me, TryCast(syntaxNode, ClassDeclaration))
                'TODO: More conditions
            Else
                'Doesn't know
                Throw New ArgumentOutOfRangeException("syntaxNode", "The syntax node provided is not any known type declaration")
            End If

            'Add this type to compile unit
            CompileUnit.AddType(newDescription)

            Return newDescription
        End Function
    End Class
End Namespace

